import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken,getUser,setUser,removeUser } from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
  state: {
    token: getToken(),
    userinfo: getUser(),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
	SET_USERINFO: (state, userinfo) => {
      state.userinfo = userinfo
	  setUser(userinfo)
    },

  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const account = new Buffer(userInfo.account.trim()).toString('base64')
      const password = userInfo.password
      const menuCode = userInfo.menuCode
      const captchaKey = userInfo.captchaKey
      return new Promise((resolve, reject) => {
        login(account, password, menuCode, captchaKey).then(res => {
			console.log("res",res)
			if(res.code==0){
				setToken(res.data.accessToken)
				commit('SET_TOKEN', res.data.accessToken)
				commit('SET_USERINFO', res.data.user)
				resolve()
			}
			
        }).catch(error => {
          reject(error)
        })
      })
    },


    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
		  commit('SET_TOKEN', '')
		  commit('SET_USERINFO', null)
		  removeToken()
		  removeUser()
		  resolve()
    //     logout(state.token).then(() => {
    //       commit('SET_TOKEN', '')
		  // commit('SET_USERINFO', null)
    //       removeToken()
    //       removeUser()
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
      })
    }
  }
}

export default user
